package view.component.weapon.bullet
{
	import enums.main.UISize;
	
	import feathers.controls.Button;
	import feathers.controls.Screen;
	import feathers.controls.ScreenNavigator;
	import feathers.controls.ScreenNavigatorItem;
	import feathers.controls.ScrollContainer;
	import feathers.controls.TabBar;
	import feathers.data.ListCollection;
	import feathers.motion.transitions.ScreenFadeTransitionManager;
	
	import starling.animation.Tween;
	import starling.core.Starling;
	import starling.events.Event;
	
	import util.FG2FUGenerator;
	
	
	/**
	 * 弹性能界面
	 * @author £iang
	 * 
	 */	
	public class BulletView extends Screen 
	{
		//-----------------------------
		// variables
		//-----------------------------
		
		/** 主选项 */
		public var mainTabBar:TabBar;
		/** 主选项滚动条容器 */
		public var mainTabSC:ScrollContainer;
		/** 主选项导航栏 */
		public var mainNavigator:ScreenNavigator;
		
		//-----------------------------
		// properties
		//-----------------------------
		
		/** 转场管理 */
		private var _transitManager:ScreenFadeTransitionManager;
		
		//-----------------------------
		// setter&getter
		//-----------------------------
		
		//-----------------------------
		// methods
		//-----------------------------
		
		/**
		 * 初始化函数 
		 * 
		 */		
		override protected function initialize():void
		{
			var labelVec:Vector.<String> = new Vector.<String>();
			var dataProvider:ListCollection = new ListCollection( [] );
			var len:int;
			var i:int;
			var obj:Object;
			
			labelVec.push(
				"弹种详细", "特殊动作", "详细数值", "伤害相关"
			);
			
			mainTabBar = new TabBar();
			mainTabBar.gap = 15 * this.dpiScale;
			len = labelVec.length;
			
			for ( i = 0; i < len; i ++ )
			{
				obj = new Object();
				obj.label = labelVec[ i ];
				dataProvider.push( obj );
			}
			
			mainTabBar.dataProvider = dataProvider;
			mainTabBar.tabFactory = mainTabFactory;
			mainTabSC = new ScrollContainer();
			this.addChild( mainTabSC );
			mainTabSC.addChild( mainTabBar );
			
			this.mainNavigator = new ScreenNavigator();
			this.addChild( mainNavigator );
			
			this.mainNavigator.addScreen(
				"INDEX0",
				new ScreenNavigatorItem( BulletTypeView )
			);
			
			this._transitManager = new ScreenFadeTransitionManager( mainNavigator );
			_transitManager.duration = 0.4;
			
			addListener();
		}
		
		/**
		 * 绘制函数 
		 * 
		 */		
		override protected function draw():void
		{
			mainTabSC.y = UISize.MAIN_H - UISize.MONSTER_TAB_AREA_H * this.dpiScale * 3 
				+ 40 * this.dpiScale;
			mainTabSC.x = 20 * this.dpiScale;
			mainTabSC.width = UISize.MAIN_W - 40 * this.dpiScale;
		}
		
		/**
		 * 添加侦听 
		 * 
		 */		
		private function addListener():void
		{
			mainTabBar.addEventListener( Event.CHANGE, tabBarChangeHandler );
		}
		
		/**
		 * 批量设置mainTabBar按钮 
		 * @return tabbar按钮
		 * 
		 */		
		public function mainTabFactory():Button
		{
			var button:Button = FG2FUGenerator.generateRoundRectButton(
				UISize.TAB_BUTTON_W2, UISize.TAB_BUTTON_H,
				this.dpiScale, UISize.TAB_BUTTON_H * 0.9, "small"
			);
			
			return button;
		}
		
		/**
		 * 清除导航栏 
		 * 
		 */		
		public function clearNavi():void
		{
			_transitManager.delay = _transitManager.duration = 0;
			_transitManager.skipNextTransition = true;
			mainNavigator.removeAllScreens();
		}
		
		
		/**
		 * 播放隐藏动画 
		 * 
		 */		
		public function playHideAnimation():void
		{
			var tween:Tween = new Tween( this, 0.4 );
			
			tween.animate( "alpha", 0);
			Starling.juggler.add( tween );
		}
		
		//-----------------------------
		// events
		//-----------------------------
		
		/**
		 * 处理tabBar改变时派发的事件 
		 * @param event tabBar改变时派发的事件 
		 * 
		 */		
		protected function tabBarChangeHandler( event:Event ):void
		{
			if ( mainTabBar.selectedIndex >= 0 )
				this.mainNavigator.showScreen( "INDEX" + mainTabBar.selectedIndex );
		}
		
		//-----------------------------
		// dispose
		//-----------------------------
		
		/**
		 * 析构函数 
		 * 
		 */		
		override public function dispose():void
		{
			Starling.juggler.removeTweens( this );
			
			var i:int;
			var button:Button;
			
			for ( i = 0; i < mainTabBar.numChildren; i ++ )
			{
				if ( mainTabBar.getChildAt( i ) is Button )
				{
					button = mainTabBar.getChildAt( i ) as Button;
					button.dispose();
					FG2FUGenerator.deepDisposeButtonComp( button );
					button = null;
				}
			}
			
			mainTabSC.dispose(); mainTabSC = null;
			
			mainNavigator.dispose();
			mainNavigator = null;
			_transitManager = null;
			super.dispose();
		}
		
		//-----------------------------
		// construction
		//-----------------------------
		
		/**
		 * 构造函数
		 *
		 */
		public function BulletView()
		{
			super();
		}
		
	}
}